﻿using ACC.Business;
using ACC.Business.Process;
using ACC.Business.Stn;
using ACC.Data;
using ACC.Data.ACCLog;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using ACC.Device.Client;
using ACC.Device.PLC.Siemens;
using ACC.Interface.Extensions;
using ACC.LineDevice.KukaPack;
using System;
using System.Text;

namespace Script
{
    /// <summary>
    /// 模组上输送线
    /// 换托盘
    /// </summary>
    public class L1M04041 : StnProcessKukaPack
    {
        public L1M04041(StnRunningData data)
            : base(data)
        {
            // 防止底层根据托盘号获取内部码
            IsPalletRelationSN = false;
        }
        protected override ACCDevice CreateDevice()
        {

            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 17000;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 17066;

                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 17002; // 17034; 
                kukaSiemensDev.DB193_ACC_Offset = 17068;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 17002; // 17034;
                kukaSiemensDev.DB194_PLC_Data_Offset = 17078;

                kukaSiemensDev.BarcodeLength = 30;
                kukaSiemensDev.LoadBarcodeCount = 2; // 1;
                kukaSiemensDev.UnloadBarcodeCount = 2; // 1;
            }

            return dev;
        }
        public override ACCCode ReadLoadParam(ACCDevice dev, out ACCDevice.LoadParam param)
        {
            ACCCode rs = base.ReadLoadParam(dev, out param);
            if (rs == ACCCode.ACCOK && !string.IsNullOrEmpty(param.UnitSN) && param.ComponentSN.Length > 0 && !string.IsNullOrEmpty(param.ComponentSN[0]))
            {
                // TO-DO: 按照接口定义，应该是02字节开始放内部码，然后34字节开始放托盘号
                PalletNo = param.ComponentSN[0];
                param.ComponentSN = new string[] { };
                this.AddPallet(PalletNo, param.UnitSN, string.Empty, string.Empty);

                //PalletNo = GetLoadunitsn(); // 新托盘号
                ////StnEventLog.Log(_data, StnLog.LogType.Event, PallteNO);
                //param.UnitSN = GetLoadPallet(); // 模组内部码
                //ChangePalletNo(PalletNo, param.UnitSN);

                WIPUnit unit = UnitDataProcess.GetWIPUnit(_lineData, param.UnitSN);
                if (unit != null)
                {
                    rs = AutoChangePartNo(unit.PartNo, ref param);
                }
            }

            return rs;
        }

        public override ACCCode ReadUnloadParam(ACCDevice dev, out ACCDevice.UnloadParam param)
        {
            ACCCode rs = base.ReadUnloadParam(dev, out param);
            // TO-DO: _data.LastLoadUnit.UnitSN 为什么不用 param.UnitSN? 从第34开始读出来的是托盘号
            // param.UnitSN = _data.LastLoadUnit.UnitSN;
            PalletNo = param.ComponentSN[0];
            param.ComponentSN = new string[] { };

            // 确定是否需要换型
            WIPUnit unit = UnitDataProcess.GetWIPUnit(_lineData, param.UnitSN);
            if (unit != null)
            {
                rs = AutoChangePartNo(unit.PartNo, ref param);
            }

            return rs;
        }

        //public bool ChangePalletNo(string pallet, string unitsn)
        //{
        //    // 解除新托盘与其他键的绑定关系
        //    // L1M03090 帖加热膜 绑定
        //    this.RemovePallet(pallet); 

        //    string sql = string.Format("UPDATE ACC_PALLETS SET PALLETNO = '{0}' WHERE SN1 = '{1}'", pallet, unitsn);
        //    int res = DataManager.DBHelper.Update(sql);
        //    if (res > 0)
        //        return true;
        //    return false;
        //}

        //public string GetLoadPallet()
        //{
        //    string _pallet = "";
        //    try
        //    {
        //        byte[] data = new byte[32];
        //        byte[] temp = new byte[30];
        //        ((SiemensDevice)_Device).ReadDBBytes(((SiemensDevice)_Device).DB194, kukaSiemensDev.DB194_PLC_Offset - 32, data);
        //        Array.Copy(data, 2, temp, 0, temp.Length);
        //        _pallet = Encoding.ASCII.GetString(temp);
        //        if (_pallet != null && _pallet.Length > data[1])
        //        {
        //            _pallet = _pallet.Substring(0, data[1]);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        logger.Error(ex.StackTrace);
        //    }
        //    return _pallet;
        //}

        //public string GetLoadunitsn()
        //{
        //    string unitsn = "";
        //    try
        //    {
        //        byte[] data = new byte[32];
        //        byte[] temp = new byte[30];
        //        ((SiemensDevice)_Device).ReadDBBytes(((SiemensDevice)_Device).DB193, kukaSiemensDev.DB193_PLC_Offset, data);
        //        Array.Copy(data, 2, temp, 0, temp.Length);
        //        unitsn = Encoding.ASCII.GetString(temp);
        //        if (unitsn != null && unitsn.Length > data[1])
        //        {
        //            unitsn = unitsn.Substring(0, data[1]);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        logger.Error(ex.StackTrace);
        //    }
        //    return unitsn;
        //}
    }
}